local skel = fk.CreateSkill{
    name = "history__huanling",
}


Fk:loadTranslationTable{
    ["history__huanling"] = "唤灵",
    [":history__huanling"] = [[出牌阶段开始时或你受到伤害后，你可以重铸一张牌，然后获得场上，弃牌堆或牌堆一张花色相同的装备牌，
    并随机出现该装备的三个兵主技能，选择其中一个令一名角色获得，若该角色不为你，你摸X张牌（X为其技能数）。]],
    ["#history__huanling-invoke"] ="唤灵：你可以选择一张牌重铸，然后获得一张花色相同的装备牌",
    [ "history_huanling_choceskill"] = "唤灵：请选择该装备的兵主技能令其他角色获得",
    ["history__huanling-choose_tar"] = "唤灵:请选择要获得%arg的角色，若不为你，你摸牌",

}

local bingzhumap={
    
  ["crossbow"] = {"zhugeliang","majun"},         -- 诸葛连弩

  ["qinggang_sword"] = {"zhaoyun","caocao"},   -- 青肛剑
      -- 寒冰剑

  ["double_swords"] = {"liubei"},    -- 雌雄双股剑

  ["blade"] = {"guanyu","guanyinping"},            -- qinglongyanyue

  ["spear"] = {"zhangfei","zhangxingcai"},            -- 丈八蛇矛

  ["axe"] = {"xuhuang"},              -- 贯石斧

  ["halberd"] = {"lvbu"},          -- 方天画戟

  ["kylin_bow"] = {"lvbu"},        -- 麒麟弓

  ["eight_diagram"] = {"zhugeliang","huangyueying"},    -- 八卦阵


  ["dilu"] = {"liubei"},             -- 的卢

  ["jueying"] = {"caocao"},          -- 绝影

  ["zhuahuangfeidian"] = {"caocao"}, -- 爪黄飞电

  ["chitu"] = {"guanyu","lvbu"},            -- 赤兔

  ["dayuan"] = {"caocao"},           -- 大苑

  ["zixing"] = {"caozhi"},           -- 紫锌


  ["guding_blade"] = {"xusheng","sunjian"},     -- 古锭刀

  ["vine"] = {"menghuo","zhurong","wutugu"},             -- 藤甲

  ["hualiu"] = {"xuhuang"},           -- 骅骝

  ["silver_lion"] = {"machao"},      -- 白银狮子

  ["fan"] = {"zhouyu"},              -- 朱雀羽扇
}

skel:addEffect(fk.Damaged, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self.name) and target == player
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local cards = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = skel.name,
          prompt = "#history__huanling-invoke",
          cancelable = true,
        })
        if #cards > 0 then
          event:setCostData(self, {cards = cards})
          return true
        end
      end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local cid = event:getCostData(self).cards[1]
        local suit = Fk:getCardById(cid):getSuitString()
        
        room:recastCard(cid, player, skel.name)
        local pat = ".|.|"..suit.."|.|.|equip"
        local getcard = room:getCardsFromPileByRule(pat, 1, "allPiles")
     
        room:obtainCard(player, getcard, true, fk.ReasonJustMove, player, skel.name)

        --if #getcard == 0 then return end
        local get = Fk:getCardById(getcard[1])
        if bingzhumap[get.name] then
            local generals = bingzhumap[get.name]
            local skills = {}
            for _, gen in ipairs(generals) do
            --根据兵主信息添加技能
  
              local samegen = {}
              if Fk.generals[gen] then
                table.insert(samegen,gen)
              end
              
              local am=Fk:getSameGenerals(gen)
            
              if #am > 0 then
                table.insertTable(samegen,am)
              end
              for _, general_name in ipairs(samegen) do
                
                local general = Fk.generals[general_name]
                for _, s in ipairs(general:getSkillNameList()) do
                    table.insertIfNeed(skills, s)
                end
              end
            end
            
            if #skills == 0 then return end
            local n =math.min(3,#skills)
            local getskill = table.random(skills,n)
            local ch = room:askToChoice(player,{
              choices = getskill,
              skill_name = skel.name,
              prompr = "history_huanling_choceskill",
              detailed = true,
            })
            if ch == "Cancel" then return end
            local to = player.room:askToChoosePlayers(player, {
              min_num = 1, max_num = 1 ,targets = room.alive_players, skill_name = skel.name,
              prompt = "history__huanling-choose_tar:::"..ch,
            })[1]
            room:handleAddLoseSkills(to, ch)
            if to ~= player then
              local x = #to:getSkillNameList()
              player:drawCards(x,skel.name)
            end

        end

    end,
})

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = skel.name,
      prompt = "#history__huanling-invoke",
      cancelable = true,
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
on_use = function(self, event, target, player, data)
    local room = player.room
    local cid = event:getCostData(self).cards[1]
    local suit = Fk:getCardById(cid):getSuitString()
    
    room:recastCard(cid, player, skel.name)
    local pat = ".|.|"..suit.."|.|.|equip"
    local getcard = room:getCardsFromPileByRule(pat, 1, "allPiles")
 
    room:obtainCard(player, getcard, true, fk.ReasonJustMove, player, skel.name)

    --if #getcard == 0 then return end
    local get = Fk:getCardById(getcard[1])
    if bingzhumap[get.name] then
        local generals = bingzhumap[get.name]
        local skills = {}
        for _, gen in ipairs(generals) do
        --根据兵主信息添加技能

          local samegen = {}
          if Fk.generals[gen] then
            table.insert(samegen,gen)
          end
          
          local am=Fk:getSameGenerals(gen)
        
          if #am > 0 then
            table.insertTable(samegen,am)
          end
          for _, general_name in ipairs(samegen) do
            local general = Fk.generals[general_name]
            for _, s in ipairs(general:getSkillNameList()) do
                table.insertIfNeed(skills, s)
            end
          end
        end
        
        if #skills == 0 then return end
        local n =math.min(3,#skills)
        local getskill = table.random(skills,n)
        local ch = room:askToChoice(player,{
          choices = getskill,
          skill_name = skel.name,
          prompr = "history_huanling_choceskill",
          detailed = true,
        })
        if ch == "Cancel" then return end
        local to = player.room:askToChoosePlayers(player, {
          min_num = 1, max_num = 1 ,targets = room.alive_players, skill_name = skel.name,
          prompt = "history__huanling-choose_tar:::"..ch,cancelable = false,
        })[1]
        room:handleAddLoseSkills(to, ch)
        if to ~= player then
          local x = #to:getSkillNameList()
          player:drawCards(x,skel.name)
        end

    end

end,
})


return skel